#include "Vector.h"
#include "Ray3.h"
#include "Geometry.h"
#include "FileWriters.h"
#include "ObjectManager.h"
#include "Matrix3.h"
#include "Color3.h"
#include "Camera.h"
#include "FileReaders.h"
#include<iostream>
using namespace std;
/*
void CornellDemoScene()
{
	//create 10 triangles
	Triangle* pTriangle = new Triangle[10];
	pTriangle[0] = Triangle(Vector3(-100.0f, -100.0f, 0.0f), Vector3(-100.0f, -100.0f, 100.0f), Vector3(100.0f, -100.0f, 100.0f));	// Floor quad
	pTriangle[1] = Triangle(Vector3(-100.0f, -100.0f, 0.0f), Vector3(100.0f, -100.0f, 100.0f), Vector3(100.0f, -100.0f, 0.0f));
	pTriangle[2] = Triangle(Vector3(-100.0f, -100.0f, 100.0f), Vector3(-100.0f, 100.0f, 100.0f), Vector3(100.0f, 100.0f, 100.0f));	// Back Wall
	pTriangle[3] = Triangle(Vector3(-100.0f, -100.0f, 100.0f), Vector3(100.0f, 100.0f, 100.0f), Vector3(100.0f, -100.0f, 100.0f));
	pTriangle[4] = Triangle(Vector3(-100.0f, -100.0f, 0.0f), Vector3(-100.0f, 100.0f, 0.0f), Vector3(-100.0f, 100.0f, 100.0f));		// left wall
	pTriangle[5] = Triangle(Vector3(-100.0f, -100.0f, 0.0f), Vector3(-100.0f, 100.0f, 100.0f), Vector3(-100.0f, -100.0f, 100.0f));
	pTriangle[6] = Triangle(Vector3(100.0f, -100.0f, 0.0f), Vector3(100.0f, -100.0f, 100.0f), Vector3(100.0f, 100.0f, 100.0f));		// right wall
	pTriangle[7] = Triangle(Vector3(100.0f, -100.0f, 0.0f), Vector3(100.0f, 100.0f, 100.0f), Vector3(100.0f, 100.0f, 0.0f));		
	pTriangle[8] = Triangle(Vector3(-100.0f, 100.0f, 0.0f), Vector3(-100.0f, 100.0f, 100.0f), Vector3(100.0f, 100.0f, 100.0f));		// Roof
	pTriangle[9] = Triangle(Vector3(-100.0f, 100.0f, 0.0f), Vector3(100.0f, 100.0f, 100.0f), Vector3(100.0f, 100.0f, 0.0f));

	//add the triangles to object list
	LinearObjManager ObjMgr;
	for(int i = 0; i < 10; i++)
		ObjMgr.InsertObject(&pTriangle[i]);

	//Set up a color array that we will use for indexing into triangles
	Color3 color_array[] = {Color3(1.0f, 1.0f, 1.0f), Color3(0.5f, 0.5f, 0.5f), Color3(1.0f, 0.0f, 0.0f), Color3(0.0f, 1.0f, 0.0f), Color3(0.25f, 0.25f, 0.25f), Color3(0.0f, 0.0f, 0.0f)};

	// Set up camera
	PinholeCamera camera(Vector3(0.0f, 0.0f, -50.0f), Vector3(0.0f, 0.0f, 1.0f), Vector3(0.0f, 1.0f, 0.0f), LHS, 50.0f, 400, 400);

	//image attribs
	int img_width = 400;
	int img_height = 400;

	//create opfile
	PPMFileWriter opfile("cornell_demo.ppm", img_width, img_height);
	Color3* pbuffer = new Color3[img_width * img_height];
	IntersectionPacket pkt;
	float t_max = 10000.0f;		// MAX
	float t1, st1, st2, st3, st4;
	
	int index = 0;


	int intersect_obj_no = 11;		//background
	for(int y = 0; y < 400; y++)
	{
		for(int x = 0; x < 400; x++)
		{
			Vector3 sample_origin, sample_direction;
			Color3 c1, c2, c3, c4, c5;
			c1 = c2 = c3 = c4 = c5 = Color3(0.0f, 0.0f, 0.0f);	// black
			t1 = st1 = st2 = st3 = st4 = t_max;	// set all to max
			Ray3 ray = camera.GetRay(x, y);
			
			//5 samples
			
			// Sample #1
			sample_origin = Vector3(ray.origin.x + 0.5f, ray.origin.y + 0.5f, ray.origin.z);
			sample_direction = camera.position - sample_origin;
			sample_direction.Normalize();
			Ray3 sample1 = Ray3(sample_origin, sample_direction);

			// Sample #2
			sample_origin = Vector3(ray.origin.x - 0.5f, ray.origin.y + 0.5f, ray.origin.z);
			sample_direction = camera.position - sample_origin;
			sample_direction.Normalize();
			Ray3 sample2 = Ray3(sample_origin, sample_direction);

			// Sample #3
			sample_origin = Vector3(ray.origin.x - 0.5f, ray.origin.y - 0.5f, ray.origin.z);
			sample_direction = camera.position - sample_origin;
			sample_direction.Normalize();
			Ray3 sample3 = Ray3(sample_origin, sample_direction);

			// Sample #4
			sample_origin = Vector3(ray.origin.x + 0.5f, ray.origin.y - 0.5f, ray.origin.z);
			sample_direction = camera.position - sample_origin;
			sample_direction.Normalize();
			Ray3 sample4 = Ray3(sample_origin, sample_direction);

			//Traverse through object list
			for(int obj_no = 0; obj_no < ObjMgr.num_obj; obj_no++)
			{
				//find nearest object
				// Main ray
				if(ObjMgr.GetObject(obj_no)->IntersectionTest(ray, 0.0f, 100.0f, pkt))
				{
					if(pkt.t < t1)	// initially t1 is set to t_max
					{
						t1 = pkt.t;
						c1 = color_array[obj_no/2];		// get color of appropriate triangle
					}
				}
				
				// Sample rays
				// #1
				if(ObjMgr.GetObject(obj_no)->IntersectionTest(sample1, 0.0f, 100.0f, pkt))
				{
					if(pkt.t < st1)	// initially t1 is set to t_max
					{
						st1 = pkt.t;
						c2 = color_array[obj_no/2];		// get color of appropriate triangle
					}
				}
				// #2
				if(ObjMgr.GetObject(obj_no)->IntersectionTest(sample2, 0.0f, 100.0f, pkt))
				{
					if(pkt.t < st2)	// initially t1 is set to t_max
					{
						st2 = pkt.t;
						c3 = color_array[obj_no/2];		// get color of appropriate triangle
					}
				}
				// #3
				if(ObjMgr.GetObject(obj_no)->IntersectionTest(sample3, 0.0f, 100.0f, pkt))
				{
					if(pkt.t < st3)	// initially t1 is set to t_max
					{
						st3 = pkt.t;
						c4 = color_array[obj_no/2];		// get color of appropriate triangle
					}
				}
				// #4
				if(ObjMgr.GetObject(obj_no)->IntersectionTest(sample4, 0.0f, 100.0f, pkt))
				{
					if(pkt.t < st4)	// initially t1 is set to t_max
					{
						st4 = pkt.t;
						c5 = color_array[obj_no/2];		// get color of appropriate triangle
					}
				}
			}

			//avg the colors
			//Color3 final = (c1+c2+c3+c4+c5)*0.2f;
			Color3 final = c1;
			pbuffer[index++] = final;
		}
	}
		
	//write file
	opfile.CopyImageBuffer(pbuffer, img_width*img_height);
	opfile.WriteFile();

	//free memory
	delete[] pbuffer;
}
*/
int main()
{
	//Demo file reading
	ObjFileReader filereader("cube1.obj");
	filereader.ParseObjFile();

	/*
	char temp[] = "123//12";
	char* token = strtok(temp, "//");
	while(token)
	{
		token = strtok(NULL, "//");
	}
	*/

	return 0;
}
